package com.aier.school.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.common.SchoolAttendanceUtil;
import com.aier.school.common.SchoolFinalPageSize;
import com.aier.school.entity.UserIcardInfo;
import com.aier.school.service.ISchoolIcCardService;

@Scope("prototype")
@Component("schoolSInOutParkAction")
public class SchoolSInOutParkAction extends SchoolBaseAction {

    /**
     * 学生进出园记录
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "classService")
    private IClassService classService;
    
    @Resource(name = "schoolIcCardService")
    private ISchoolIcCardService schoolIcCardService;    
    
    private List<Classes> schoolClassList;        //学校班级列表(供下拉选择)
    private List<UserIcardInfo> stuIcardInfList;//进出园记录列表

    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    private Integer sid;            //学校id
    private Long cid;                //班级id
    private String dayEndValue;     //开始时间
    private String dayBeginValue;   //结束时间
    
    private int page;                //页数
    private int page_size;            //每页的记录数
    private int page_count;            //总页数
    private int count;                //总数

    private ByteArrayInputStream inputExcel;
    private String filename;
    
    @Override
    public String execute() {
        schoolClassList = classService.searchClassList(sid);
        return SUCCESS;
    }

    @SuppressWarnings("unchecked")
    public String searchUIIList(){
        s = 0;
        if(page<=0){
            page=1;
        }

        Map<String,Object> map = schoolIcCardService.searchUserIcardInfoListByCondation(page, SchoolFinalPageSize.ATTEND_DETAIL_LIST_PAGESIZE, 
                sid, cid, dayBeginValue, dayEndValue,null);
        if(null!=map){
            stuIcardInfList = (List<UserIcardInfo>) map.get(ICommonDAO.OBJ_LIST);
            if (null != stuIcardInfList) {
                s = 1;
            }
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            
        }
        return SUCCESS;
    }
    
    public String down() {
        
        List<UserIcardInfo> icardInfoList = schoolIcCardService.searchUserIcardInfoListByCondation(
                sid, cid, dayBeginValue, dayEndValue,null);
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        
        try {
            SchoolAttendanceUtil.createUIIExcel(icardInfoList).write(out);
        } catch (IOException e) {
            return null;
        } catch (Exception e){
            e.printStackTrace();
        }
        inputExcel = new ByteArrayInputStream(out.toByteArray());
        filename="student_iccard_info.xls";
        return SUCCESS;        
    }
    
    
    
    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public List<Classes> getSchoolClassList() {
        return schoolClassList;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public List<UserIcardInfo> getStuIcardInfList() {
        return stuIcardInfList;
    }

    public int getCount() {
        return count;
    }

    public void setSchoolIcCardService(ISchoolIcCardService schoolIcCardService) {
        this.schoolIcCardService = schoolIcCardService;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setDayEndValue(String dayEndValue) {
        this.dayEndValue = dayEndValue;
    }

    public void setDayBeginValue(String dayBeginValue) {
        this.dayBeginValue = dayBeginValue;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }
    
    public ByteArrayInputStream getInputExcel() {
        return inputExcel;
    }
    public void setInputExcel(ByteArrayInputStream inputExcel) {
        this.inputExcel = inputExcel;
    }
    public String getFilename() {
        return filename;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }
}
